unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ScktComp, StdCtrls, strutils, ExtCtrls;

type
  TFrmMain = class(TForm)
    Tournament: TMemo;
    ServerSocket1: TServerSocket;
    Table: TMemo;
    Event: TMemo;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Clients: TMemo;
    tmr_CheckActiveClients: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
    procedure tmr_CheckActiveClientsTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmMain: TFrmMain;

implementation

uses uutils;
{$R *.dfm}

procedure TFrmMain.FormCreate(Sender: TObject);
begin
  ServerSocket1.Port := 7781;
  ServerSocket1.Active := True;
end;

procedure TFrmMain.FormDestroy(Sender: TObject);
begin
  ServerSocket1.Active := false;
end;

procedure TFrmMain.ServerSocket1ClientConnect(Sender: TObject; Socket: TCustomWinSocket);
var
  i: Integer;
begin
  Clients.Lines.Clear;
  for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
  begin
    with ServerSocket1.Socket.Connections[i] do
    begin
      Clients.Lines.Add(ServerSocket1.Socket.Connections[i].RemoteHost);
    end;
  end;
end;

procedure TFrmMain.ServerSocket1ClientDisconnect(Sender: TObject; Socket: TCustomWinSocket);
var
  i: Integer;
begin
  tmr_CheckActiveClients.Enabled := True;
end;

procedure TFrmMain.ServerSocket1ClientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  ErrorCode := 0; // this line solve your problems about asyncroneus socket error
end;

procedure TFrmMain.ServerSocket1ClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  i: Integer;
  sRec: string;
  EventType, TournamentID, TableID: string;
begin
  sRec := Socket.ReceiveText;
  if sRec <> '' then
  begin
    uutils.SocketTextParse(sRec, EventType, TournamentID, TableID);
    Event.Lines.Add(EventType);
    Tournament.Lines.Add(TournamentID);
    Table.Lines.Add(TableID);
    for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
    begin
      if Socket.Handle <> ServerSocket1.Socket.Connections[i].Handle then
      begin
        // EventType + '|1|' + TournamentID + '|2|' + TableID
        ServerSocket1.Socket.Connections[i].SendText(sRec);
      end;
    end;
  end;
end;

procedure TFrmMain.tmr_CheckActiveClientsTimer(Sender: TObject);
var
  i: Integer;
begin
  tmr_CheckActiveClients.Enabled := false;
  Clients.Lines.Clear;
  Event.Lines.Clear;
  Table.Lines.Clear;
  Tournament.Lines.Clear;
  for i := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
  begin
    with ServerSocket1.Socket.Connections[i] do
    begin
      Clients.Lines.Add(ServerSocket1.Socket.Connections[i].RemoteHost);
    end;
  end;
end;

end.
